/**
 * Zhz.net Inc.
 * Copyright (c) 2004-2010 All Rights Reserved.
 */
package com.zhz.lib.project.controller;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhz.lib.project.hibernate.Sortmap;
import com.zhz.lib.project.service.faq.FaqService;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;
import com.zhz.project.common.util.zhzutil.ZhzStringUtil;

/**
 * 
 * @author Administrator
 * @version $Id: FaqController.java, v 0.1 Sep 16, 2010 7:55:57 AM Administrator
 *          Exp $
 */

@Controller
@RequestMapping("/faq.htm")
public class FaqController {
	private static Logger logger = Logger.getLogger(FaqController.class);
	@Autowired
	private FaqService faqService;
	@Autowired
	private IbatisPage ibatisPage;

	private static String pageSql = "selectAllFaqByPage";
	private static String countSql = "selectAllFaqCount";

	/**
	 * 常见问题列表(Ibatis)
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public String getIbatisList(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		try {
			// 传递给Ibatis的map
			Map map = new HashMap();

			// 当前页
			String indexPage = ZhzHttpRequestUtil.getParameters(request,
					"indexPage");
			Integer pageNo = null;
			if (StringUtil.isNotBlank(indexPage)) {
				pageNo = new Integer(indexPage);
			}
			map.put("indexPage", pageNo);

			// 搜索框的值，因为已经编码，因此这里不再转码了
			String keyword = ZhzHttpRequestUtil.getParameters(request,
					"keyword");
			String keywordOut = null;
			if (StringUtil.isNotBlank(keyword)) {
				keyword = URLDecoder.decode(keyword);
				keywordOut = URLEncoder.encode(URLEncoder.encode(keyword));
			}
			if (StringUtil.isBlank(keyword)
					|| StringUtil.equals(keyword, "null")) {
				keyword = null;
			} else {
				map.put("question", keyword);
			}
			// 常见问题类型
			String sortMapId = ZhzHttpRequestUtil.getParameters(request,
					"sortMapId");
			if (StringUtil.isBlank(sortMapId)
					|| StringUtil.equals(sortMapId, "null")) {
				sortMapId = null;
			} else {
				map.put("sortMapId", sortMapId);
			}
			ZhzHttpRequestUtil.writeMapToModelMap(model, ibatisPage.getPage(
					map, pageSql, countSql));

			// 获取常见问题类型
			List<Sortmap> typeList = faqService.getFaqType();
			model.put("typeList", typeList);

			model.put("keywordOut", keywordOut);
			model.put("keyword", keyword);
			model.put("sortMapId", sortMapId);

			return "faq";
		} catch (Exception e) {
			logger.error(e.toString());
			return null;
		}

	}

	/**
	 * 
	 * 根据id获取常见问题
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, params = "event=single")
	public String getSingleNews(ModelMap model, HttpServletRequest request,
			HttpServletResponse response, @RequestParam("id") int id) {
		model.put("faq", faqService.getFaqById(id));
		return "faqDetail";
	}

}
